﻿@model IPagedList<Category>
@{
    ViewBag.Title = "Manage Categories";
}

@helper CategoryRow(Category category, int level = 0) {
    <tr id="@category.CategoryId">
        <td>
            <div style="margin-left: @(level*15)px">
                <a href="@category.Link">@category.Name</a><br />
                <span class="gray">/@category.Slug</span>
            </div>
        </td>
        <td>
            @category.Snippets.Count snippet(s)
        </td>
        <td class="alignRight">
            @Html.ActionLink("Edit", "Edit", new { id = category.CategoryId }, new { @class = "lightButton" }) 
            @Html.ActionLink("Delete", "Delete", new { id = category.CategoryId }, new { @class = "lightButton delete" })
        </td>
    </tr>
    if (ViewBag.IsSearching && category.ChildrenCategories.Any()) {
        level = level + 1;
        foreach (var cat in category.ChildrenCategories.Where(cat => !cat.IsDeleted)) {
            @CategoryRow(cat, level)
        }
    }
}

<h1>Categories</h1>

@Html.Partial("_CategoryMenu")

<table class="full">
    <thead>
        <tr>
            <th id="usersnameColumn">Category</th>
            <th>Snippets</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
    @if (Model.Count == 0) {
        <tr>
            <td colspan="4">No categories found.</td>
        </tr>
    }
    @foreach (var category in Model) {
        @CategoryRow(category)
    }
    </tbody>
    <tfoot>
        <tr>
            <td colspan="4" class="alignRight">
                @if (Model.PageCount > 1) {
                    @Html.Pager(Model)
                }
            </td>
        </tr>
    </tfoot>
</table>

<script src="@Url.Content("~/Assets/Scripts/lib/jquery.undoable.js")" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $('a.delete').undoable({
            url: '@Url.Action("Delete", "Categories")',
            getPostData: function (clickSource, target) {
                return {
                    id: clickSource.parents('tr').attr('id')
                };
            }
        });
    });
</script>